home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / jpeglib5b / jquant2.c < prev    next >
C/C++ Source or Header  |  1980-01-12  |  46KB  |  1,254 lines

  1. /*
  2.  * jquant2.c
  3.  *
  4.  * Copyright (C) 1991-1994, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains 2-pass color quantization (color mapping) routines.
  9.  * These routines provide selection of a custom color map for an image,
  10.  * followed by mapping of the image to that color map, with optional
  11.  * Floyd-Steinberg dithering.
  12.  * It is also possible to use just the second pass to map to an arbitrary
  13.  * externally-given color map.
  14.  *
  15.  * Note: ordered dithering is not supported, since there isn't any fast
  16.  * way to compute intercolor distances; it's unclear that ordered dither's
  17.  * fundamental assumptions even hold with an irregularly spaced color map.
  18.  */
  19.  
  20. #define JPEG_INTERNALS
  21. #include "jinclude.h"
  22. #include "jpeglib.h"
  23.  
  24. #ifdef QUANT_2PASS_SUPPORTED
  25.  
  26.  
  27. /*
  28.  * This module implements the well-known Heckbert paradigm for color
  29.  * quantization.  Most of the ideas used here can be traced back to
  30.  * Heckbert's seminal paper
  31.  *   Heckbert, Paul.  "Color Image Quantization for Frame Buffer Display",
  32.  *   Proc. SIGGRAPH '82, Computer Graphics v.16 #3 (July 1982), pp 297-304.
  33.  *
  34.  * In the first pass over the image, we accumulate a histogram showing the
  35.  * usage count of each possible color.  To keep the histogram to a reasonable
  36.  * size, we reduce the precision of the input; typical practice is to retain
  37.  * 5 or 6 bits per color, so that 8 or 4 different input values are counted
  38.  * in the same histogram cell.
  39.  *
  40.  * Next, the color-selection step begins with a box representing the whole
  41.  * color space, and repeatedly splits the "largest" remaining box until we
  42.  * have as many boxes as desired colors.  Then the mean color in each
  43.  * remaining box becomes one of the possible output colors.
  44.  * 
  45.  * The second pass over the image maps each input pixel to the closest output
  46.  * color (optionally after applying a Floyd-Steinberg dithering correction).
  47.  * This mapping is logically trivial, but making it go fast enough requires
  48.  * considerable care.
  49.  *
  50.  * Heckbert-style quantizers vary a good deal in their policies for choosing
  51.  * the "largest" box and deciding where to cut it.  The particular policies
  52.  * used here have proved out well in experimental comparisons, but better ones
  53.  * may yet be found.
  54.  *
  55.  * In earlier versions of the IJG code, this module quantized in YCbCr color
  56.  * space, processing the raw upsampled data without a color conversion step.
  57.  * This allowed the color conversion math to be done only once per colormap
  58.  * entry, not once per pixel.  However, that optimization precluded other
  59.  * useful optimizations (such as merging color conversion with upsampling)
  60.  * and it also interfered with desired capabilities such as quantizing to an
  61.  * externally-supplied colormap.  We have therefore abandoned that approach.
  62.  * The present code works in the post-conversion color space, typically RGB.
  63.  *
  64.  * To improve the visual quality of the results, we actually work in scaled
  65.  * RGB space, giving G distances more weight than R, and R in turn more than
  66.  * B.  To do everything in integer math, we must use integer scale factors.
  67.  * The 2/3/1 scale factors used here correspond loosely to the relative
  68.  * weights of the colors in the NTSC grayscale equation.
  69.  * If you want to use this code to quantize a non-RGB color space, you'll
  70.  * probably need to change these scale factors.
  71.  */
  72.  
  73. #define R_SCALE 2        /* scale R distances by this much */
  74. #define G_SCALE 3        /* scale G distances by this much */
  75. #define B_SCALE 1        /* and B by this much */
  76.  
  77. /* Relabel R/G/B as components 0/1/2, respecting the RGB ordering defined
  78.  * in jmorecfg.h.  As the code stands, it will do the right thing for R,G,B
  79.  * and B,G,R orders.  If you define some other weird order in jmorecfg.h,
  80.  * you'll get compile errors until you extend this logic.  In that case
  81.  * you'll probably want to tweak the histogram sizes too.
  82.  */
  83.  
  84. #if RGB_RED == 0
  85. #define C0_SCALE R_SCALE
  86. #endif
  87. #if RGB_BLUE == 0
  88. #define C0_SCALE B_SCALE
  89. #endif
  90. #if RGB_GREEN == 1
  91. #define C1_SCALE G_SCALE
  92. #endif
  93. #if RGB_RED == 2
  94. #define C2_SCALE R_SCALE
  95. #endif
  96. #if RGB_BLUE == 2
  97. #define C2_SCALE B_SCALE
  98. #endif
  99.  
  100.  
  101. /*
  102.  * First we have the histogram data structure and routines for creating it.
  103.  *
  104.  * The number of bits of precision can be adjusted by changing these symbols.
  105.  * We recommend keeping 6 bits for G and 5 each for R and B.
  106.  * If you have plenty of memory and cycles, 6 bits all around gives marginally
  107.  * better results; if you are short of memory, 5 bits all around will save
  108.  * some space but degrade the results.
  109.  * To maintain a fully accurate histogram, we'd need to allocate a "long"
  110.  * (preferably unsigned long) for each cell.  In practice this is overkill;
  111.  * we can get by with 16 bits per cell.  Few of the cell counts will overflow,
  112.  * and clamping those that do overflow to the maximum value will give close-
  113.  * enough results.  This reduces the recommended histogram size from 256Kb
  114.  * to 128Kb, which is a useful savings on PC-class machines.
  115.  * (In the second pass the histogram space is re-used for pixel mapping data;
  116.  * in that capacity, each cell must be able to store zero to the number of
  117.  * desired colors.  16 bits/cell is plenty for that too.)
  118.  * Since the JPEG code is intended to run in small memory model on 80x86
  119.  * machines, we can't just allocate the histogram in one chunk.  Instead
  120.  * of a true 3-D array, we use a row of pointers to 2-D arrays.  Each
  121.  * pointer corresponds to a C0 value (typically 2^5 = 32 pointers) and
  122.  * each 2-D array has 2^6*2^5 = 2048 or 2^6*2^6 = 4096 entries.  Note that
  123.  * on 80x86 machines, the pointer row is in near memory but the actual
  124.  * arrays are in far memory (same arrangement as we use for image arrays).
  125.  */
  126.  
  127. #define MAXNUMCOLORS  (MAXJSAMPLE+1) /* maximum size of colormap */
  128.  
  129. /* These will do the right thing for either R,G,B or B,G,R color order,
  130.  * but you may not like the results for other color orders.
  131.  */
  132. #define HIST_C0_BITS  5        /* bits of precision in R/B histogram */
  133. #define HIST_C1_BITS  6        /* bits of precision in G histogram */
  134. #define HIST_C2_BITS  5        /* bits of precision in B/R histogram */
  135.  
  136. /* Number of elements along histogram axes. */
  137. #define HIST_C0_ELEMS  (1<<HIST_C0_BITS)
  138. #define HIST_C1_ELEMS  (1<<HIST_C1_BITS)
  139. #define HIST_C2_ELEMS  (1<<HIST_C2_BITS)
  140.  
  141. /* These are the amounts to shift an input value to get a histogram index. */
  142. #define C0_SHIFT  (BITS_IN_JSAMPLE-HIST_C0_BITS)
  143. #define C1_SHIFT  (BITS_IN_JSAMPLE-HIST_C1_BITS)
  144. #define C2_SHIFT  (BITS_IN_JSAMPLE-HIST_C2_BITS)
  145.  
  146.  
  147. typedef UINT16 histcell;    /* histogram cell; prefer an unsigned type */
  148.  
  149. typedef histcell FAR * histptr;    /* for pointers to histogram cells */
  150.  
  151. typedef histcell hist1d[HIST_C2_ELEMS]; /* typedefs for the array */
  152. typedef hist1d FAR * hist2d;    /* type for the 2nd-level pointers */
  153. typedef hist2d * hist3d;    /* type for top-level pointer */
  154.  
  155.  
  156. /* Declarations for Floyd-Steinberg dithering.
  157.  *
  158.  * Errors are accumulated into the array fserrors[], at a resolution of
  159.  * 1/16th of a pixel count.  The error at a given pixel is propagated
  160.  * to its not-yet-processed neighbors using the standard F-S fractions,
  161.  *        ...    (here)    7/16
  162.  *        3/16    5/16    1/16
  163.  * We work left-to-right on even rows, right-to-left on odd rows.
  164.  *
  165.  * We can get away with a single array (holding one row's worth of errors)
  166.  * by using it to store the current row's errors at pixel columns not yet
  167.  * processed, but the next row's errors at columns already processed.  We
  168.  * need only a few extra variables to hold the errors immediately around the
  169.  * current column.  (If we are lucky, those variables are in registers, but
  170.  * even if not, they're probably cheaper to access than array elements are.)
  171.  *
  172.  * The fserrors[] array has (#columns + 2) entries; the extra entry at
  173.  * each end saves us from special-casing the first and last pixels.
  174.  * Each entry is three values long, one value for each color component.
  175.  *
  176.  * Note: on a wide image, we might not have enough room in a PC's near data
  177.  * segment to hold the error array; so it is allocated with alloc_large.
  178.  */
  179.  
  180. #if BITS_IN_JSAMPLE == 8
  181. typedef INT16 FSERROR;        /* 16 bits should be enough */
  182. typedef int LOCFSERROR;        /* use 'int' for calculation temps */
  183. #else
  184. typedef INT32 FSERROR;        /* may need more than 16 bits */
  185. typedef INT32 LOCFSERROR;    /* be sure calculation temps are big enough */
  186. #endif
  187.  
  188. typedef FSERROR FAR *FSERRPTR;    /* pointer to error array (in FAR storage!) */
  189.  
  190.  
  191. /* Private subobject */
  192.  
  193. typedef struct {
  194.   struct jpeg_color_quantizer pub; /* public fields */
  195.  
  196.   /* Variables for accumulating image statistics */
  197.   hist3d histogram;        /* pointer to the histogram */
  198.  
  199.   /* Variables for Floyd-Steinberg dithering */
  200.   FSERRPTR fserrors;        /* accumulated errors */
  201.   boolean on_odd_row;        /* flag to remember which row we are on */
  202.   int * error_limiter;        /* table for clamping the applied error */
  203. } my_cquantizer;
  204.  
  205. typedef my_cquantizer * my_cquantize_ptr;
  206.  
  207.  
  208. /*
  209.  * Prescan some rows of pixels.
  210.  * In this module the prescan simply updates the histogram, which has been
  211.  * initialized to zeroes by start_pass.
  212.  * An output_buf parameter is required by the method signature, but no data
  213.  * is actually output (in fact the buffer controller is probably passing a
  214.  * NULL pointer).
  215.  */
  216.  
  217. METHODDEF void
  218. prescan_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
  219.           JSAMPARRAY output_buf, int num_rows)
  220. {
  221.   my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  222.   register JSAMPROW ptr;
  223.   register histptr histp;
  224.   register hist3d histogram = cquantize->histogram;
  225.   int row;
  226.   JDIMENSION col;
  227.   JDIMENSION width = cinfo->output_width;
  228.  
  229.   for (row = 0; row < num_rows; row++) {
  230.     ptr = input_buf[row];
  231.     for (col = width; col > 0; col--) {
  232.       /* get pixel value and index into the histogram */
  233.       histp = & histogram[GETJSAMPLE(ptr[0]) >> C0_SHIFT]
  234.              [GETJSAMPLE(ptr[1]) >> C1_SHIFT]
  235.              [GETJSAMPLE(ptr[2]) >> C2_SHIFT];
  236.       /* increment, check for overflow and undo increment if so. */
  237.       if (++(*histp) <= 0)
  238.     (*histp)--;
  239.       ptr += 3;
  240.     }
  241.   }
  242. }
  243.  
  244.  
  245. /*
  246.  * Next we have the really interesting routines: selection of a colormap
  247.  * given the completed histogram.
  248.  * These routines work with a list of "boxes", each representing a rectangular
  249.  * subset of the input color space (to histogram precision).
  250.  */
  251.  
  252. typedef struct {
  253.   /* The bounds of the box (inclusive); expressed as histogram indexes */
  254.   int c0min, c0max;
  255.   int c1min, c1max;
  256.   int c2min, c2max;
  257.   /* The volume (actually 2-norm) of the box */
  258.   INT32 volume;
  259.   /* The number of nonzero histogram cells within this box */
  260.   long colorcount;
  261. } box;
  262.  
  263. typedef box * boxptr;
  264.  
  265.  
  266. LOCAL boxptr
  267. find_biggest_color_pop (boxptr boxlist, int numboxes)
  268. /* Find the splittable box with the largest color population */
  269. /* Returns NULL if no splittable boxes remain */
  270. {
  271.   register boxptr boxp;
  272.   register int i;
  273.   register long maxc = 0;
  274.   boxptr which = NULL;
  275.   
  276.   for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
  277.     if (boxp->colorcount > maxc && boxp->volume > 0) {
  278.       which = boxp;
  279.       maxc = boxp->colorcount;
  280.     }
  281.   }
  282.   return which;
  283. }
  284.  
  285.  
  286. LOCAL boxptr
  287. find_biggest_volume (boxptr boxlist, int numboxes)
  288. /* Find the splittable box with the largest (scaled) volume */
  289. /* Returns NULL if no splittable boxes remain */
  290. {
  291.   register boxptr boxp;
  292.   register int i;
  293.   register INT32 maxv = 0;
  294.   boxptr which = NULL;
  295.   
  296.   for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
  297.     if (boxp->volume > maxv) {
  298.       which = boxp;
  299.       maxv = boxp->volume;
  300.     }
  301.   }
  302.   return which;
  303. }
  304.  
  305.  
  306. LOCAL void
  307. update_box (j_decompress_ptr cinfo, boxptr boxp)
  308. /* Shrink the min/max bounds of a box to enclose only nonzero elements, */
  309. /* and recompute its volume and population */
  310. {
  311.   my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  312.   hist3d histogram = cquantize->histogram;
  313.   histptr histp;
  314.   int c0,c1,c2;
  315.   int c0min,c0max,c1min,c1max,c2min,c2max;
  316.   INT32 dist0,dist1,dist2;
  317.   long ccount;
  318.   
  319.   c0min = boxp->c0min;  c0max = boxp->c0max;
  320.   c1min = boxp->c1min;  c1max = boxp->c1max;
  321.   c2min = boxp->c2min;  c2max = boxp->c2max;
  322.   
  323.   if (c0max > c0min)
  324.     for (c0 = c0min; c0 <= c0max; c0++)
  325.       for (c1 = c1min; c1 <= c1max; c1++) {
  326.     histp = & histogram[c0][c1][c2min];
  327.     for (c2 = c2min; c2 <= c2max; c2++)
  328.       if (*histp++ != 0) {
  329.         boxp->c0min = c0min = c0;
  330.         goto have_c0min;
  331.       }
  332.       }
  333.  have_c0min:
  334.   if (c0max > c0min)
  335.     for (c0 = c0max; c0 >= c0min; c0--)
  336.       for (c1 = c1min; c1 <= c1max; c1++) {
  337.     histp = & histogram[c0][c1][c2min];
  338.     for (c2 = c2min; c2 <= c2max; c2++)
  339.       if (*histp++ != 0) {
  340.         boxp->c0max = c0max = c0;
  341.         goto have_c0max;
  342.       }
  343.       }
  344.  have_c0max:
  345.   if (c1max > c1min)
  346.     for (c1 = c1min; c1 <= c1max; c1++)
  347.       for (c0 = c0min; c0 <= c0max; c0++) {
  348.     histp = & histogram[c0][c1][c2min];
  349.     for (c2 = c2min; c2 <= c2max; c2++)
  350.       if (*histp++ != 0) {
  351.         boxp->c1min = c1min = c1;
  352.         goto have_c1min;
  353.       }
  354.       }
  355.  have_c1min:
  356.   if (c1max > c1min)
  357.     for (c1 = c1max; c1 >= c1min; c1--)
  358.       for (c0 = c0min; c0 <= c0max; c0++) {
  359.     histp = & histogram[c0][c1][c2min];
  360.     for (c2 = c2min; c2 <= c2max; c2++)
  361.       if (*histp++ != 0) {
  362.         boxp->c1max = c1max = c1;
  363.         goto have_c1max;
  364.       }
  365.       }
  366.  have_c1max:
  367.   if (c2max > c2min)
  368.     for (c2 = c2min; c2 <= c2max; c2++)
  369.       for (c0 = c0min; c0 <= c0max; c0++) {
  370.     histp = & histogram[c0][c1min][c2];
  371.     for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
  372.       if (*histp != 0) {
  373.         boxp->c2min = c2min = c2;
  374.         goto have_c2min;
  375.       }
  376.       }
  377.  have_c2min:
  378.   if (c2max > c2min)
  379.     for (c2 = c2max; c2 >= c2min; c2--)
  380.       for (c0 = c0min; c0 <= c0max; c0++) {
  381.     histp = & histogram[c0][c1min][c2];
  382.     for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
  383.       if (*histp != 0) {
  384.         boxp->c2max = c2max = c2;
  385.         goto have_c2max;
  386.       }
  387.       }
  388.  have_c2max:
  389.  
  390.   /* Update box volume.
  391.    * We use 2-norm rather than real volume here; this biases the method
  392.    * against making long narrow boxes, and it has the side benefit that
  393.    * a box is splittable iff norm > 0.
  394.    * Since the differences are expressed in histogram-cell units,
  395.    * we have to shift back to JSAMPLE units to get consistent distances;
  396.    * after which, we scale according to the selected distance scale factors.
  397.    */
  398.   dist0 = ((c0max - c0min) << C0_SHIFT) * C0_SCALE;
  399.   dist1 = ((c1max - c1min) << C1_SHIFT) * C1_SCALE;
  400.   dist2 = ((c2max - c2min) << C2_SHIFT) * C2_SCALE;
  401.   boxp->volume = dist0*dist0 + dist1*dist1 + dist2*dist2;
  402.   
  403.   /* Now scan remaining volume of box and compute population */
  404.   ccount = 0;
  405.   for (c0 = c0min; c0 <= c0max; c0++)
  406.     for (c1 = c1min; c1 <= c1max; c1++) {
  407.       histp = & histogram[c0][c1][c2min];
  408.       for (c2 = c2min; c2 <= c2max; c2++, histp++)
  409.     if (*histp != 0) {
  410.       ccount++;
  411.     }
  412.     }
  413.   boxp->colorcount = ccount;
  414. }
  415.  
  416.  
  417. LOCAL int
  418. median_cut (j_decompress_ptr cinfo, boxptr boxlist, int numboxes,
  419.         int desired_colors)
  420. /* Repeatedly select and split the largest box until we have enough boxes */
  421. {
  422.   int n,lb;
  423.   int c0,c1,c2,cmax;
  424.   register boxptr b1,b2;
  425.  
  426.   while (numboxes < desired_colors) {
  427.     /* Select box to split.
  428.      * Current algorithm: by population for first half, then by volume.
  429.      */
  430.     if (numboxes*2 <= desired_colors) {
  431.       b1 = find_biggest_color_pop(boxlist, numboxes);
  432.     } else {
  433.       b1 = find_biggest_volume(boxlist, numboxes);
  434.     }
  435.     if (b1 == NULL)        /* no splittable boxes left! */
  436.       break;
  437.     b2 = &boxlist[numboxes];    /* where new box will go */
  438.     /* Copy the color bounds to the new box. */
  439.     b2->c0max = b1->c0max; b2->c1max = b1->c1max; b2->c2max = b1->c2max;
  440.     b2->c0min = b1->c0min; b2->c1min = b1->c1min; b2->c2min = b1->c2min;
  441.     /* Choose which axis to split the box on.
  442.      * Current algorithm: longest scaled axis.
  443.      * See notes in update_box about scaling distances.
  444.      */
  445.     c0 = ((b1->c0max - b1->c0min) << C0_SHIFT) * C0_SCALE;
  446.     c1 = ((b1->c1max - b1->c1min) << C1_SHIFT) * C1_SCALE;
  447.     c2 = ((b1->c2max - b1->c2min) << C2_SHIFT) * C2_SCALE;
  448.     /* We want to break any ties in favor of green, then red, blue last.
  449.      * This code does the right thing for R,G,B or B,G,R color orders only.
  450.      */
  451. #if RGB_RED == 0
  452.     cmax = c1; n = 1;
  453.     if (c0 > cmax) { cmax = c0; n = 0; }
  454.     if (c2 > cmax) { n = 2; }
  455. #else
  456.     cmax = c1; n = 1;
  457.     if (c2 > cmax) { cmax = c2; n = 2; }
  458.     if (c0 > cmax) { n = 0; }
  459. #endif
  460.     /* Choose split point along selected axis, and update box bounds.
  461.      * Current algorithm: split at halfway point.
  462.      * (Since the box has been shrunk to minimum volume,
  463.      * any split will produce two nonempty subboxes.)
  464.      * Note that lb value is max for lower box, so must be < old max.
  465.      */
  466.     switch (n) {
  467.     case 0:
  468.       lb = (b1->c0max + b1->c0min) / 2;
  469.       b1->c0max = lb;
  470.       b2->c0min = lb+1;
  471.       break;
  472.     case 1:
  473.       lb = (b1->c1max + b1->c1min) / 2;
  474.       b1->c1max = lb;
  475.       b2->c1min = lb+1;
  476.       break;
  477.     case 2:
  478.       lb = (b1->c2max + b1->c2min) / 2;
  479.       b1->c2max = lb;
  480.       b2->c2min = lb+1;
  481.       break;
  482.     }
  483.     /* Update stats for boxes */
  484.     update_box(cinfo, b1);
  485.     update_box(cinfo, b2);
  486.     numboxes++;
  487.   }
  488.   return numboxes;
  489. }
  490.  
  491.  
  492. LOCAL void
  493. compute_color (j_decompress_ptr cinfo, boxptr boxp, int icolor)
  494. /* Compute representative color for a box, put it in colormap[icolor] */
  495. {
  496.   /* Current algorithm: mean weighted by pixels (not colors) */
  497.   /* Note it is important to get the rounding correct! */
  498.   my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  499.   hist3d histogram = cquantize->histogram;
  500.   histptr histp;
  501.   int c0,c1,c2;
  502.   int c0min,c0max,c1min,c1max,c2min,c2max;
  503.   long count;
  504.   long total = 0;
  505.   long c0total = 0;
  506.   long c1total = 0;
  507.   long c2total = 0;
  508.   
  509.   c0min = boxp->c0min;  c0max = boxp->c0max;
  510.   c1min = boxp->c1min;  c1max = boxp->c1max;
  511.   c2min = boxp->c2min;  c2max = boxp->c2max;
  512.   
  513.   for (c0 = c0min; c0 <= c0max; c0++)
  514.     for (c1 = c1min; c1 <= c1max; c1++) {
  515.       histp = & histogram[c0][c1][c2min];
  516.       for (c2 = c2min; c2 <= c2max; c2++) {
  517.     if ((count = *histp++) != 0) {
  518.       total += count;
  519.       c0total += ((c0 << C0_SHIFT) + ((1<<C0_SHIFT)>>1)) * count;
  520.       c1total += ((c1 << C1_SHIFT) + ((1<<C1_SHIFT)>>1)) * count;
  521.       c2total += ((c2 << C2_SHIFT) + ((1<<C2_SHIFT)>>1)) * count;
  522.     }
  523.       }
  524.     }
  525.   
  526.   cinfo->colormap[0][icolor] = (JSAMPLE) ((c0total + (total>>1)) / total);
  527.   cinfo->colormap[1][icolor] = (JSAMPLE) ((c1total + (total>>1)) / total);
  528.   cinfo->colormap[2][icolor] = (JSAMPLE) ((c2total + (total>>1)) / total);
  529. }
  530.  
  531.  
  532. LOCAL void
  533. select_colors (j_decompress_ptr cinfo)
  534. /* Master routine for color selection */
  535. {
  536.   boxptr boxlist;
  537.   int numboxes;
  538.   int desired = cinfo->desired_number_of_colors;
  539.   int i;
  540.  
  541.   /* Allocate workspace for box list */
  542.   boxlist = (boxptr) (*cinfo->mem->alloc_small)
  543.     ((j_common_ptr) cinfo, JPOOL_IMAGE, desired * SIZEOF(box));
  544.   /* Initialize one box containing whole space */
  545.   numboxes = 1;
  546.   boxlist[0].c0min = 0;
  547.   boxlist[0].c0max = MAXJSAMPLE >> C0_SHIFT;
  548.   boxlist[0].c1min = 0;
  549.   boxlist[0].c1max = MAXJSAMPLE >> C1_SHIFT;
  550.   boxlist[0].c2min = 0;
  551.   boxlist[0].c2max = MAXJSAMPLE >> C2_SHIFT;
  552.   /* Shrink it to actually-used volume and set its statistics */
  553.   update_box(cinfo, & boxlist[0]);
  554.   /* Perform median-cut to produce final box list */
  555.   numboxes = median_cut(cinfo, boxlist, numboxes, desired);
  556.   /* Compute the representative color for each box, fill colormap */
  557.   for (i = 0; i < numboxes; i++)
  558.     compute_color(cinfo, & boxlist[i], i);
  559.   cinfo->actual_number_of_colors = numboxes;
  560.   TRACEMS1(cinfo, 1, JTRC_QUANT_SELECTED, numboxes);
  561. }
  562.  
  563.  
  564. /*
  565.  * These routines are concerned with the time-critical task of mapping input
  566.  * colors to the nearest color in the selected colormap.
  567.  *
  568.  * We re-use the histogram space as an "inverse color map", essentially a
  569.  * cache for the results of nearest-color searches.  All colors within a
  570.  * histogram cell will be mapped to the same colormap entry, namely the one
  571.  * closest to the cell's center.  This may not be quite the closest entry to
  572.  * the actual input color, but it's almost as good.  A zero in the cache
  573.  * indicates we haven't found the nearest color for that cell yet; the array
  574.  * is cleared to zeroes before starting the mapping pass.  When we find the
  575.  * nearest color for a cell, its colormap index plus one is recorded in the
  576.  * cache for future use.  The pass2 scanning routines call fill_inverse_cmap
  577.  * when they need to use an unfilled entry in the cache.
  578.  *
  579.  * Our method of efficiently finding nearest colors is based on the "locally
  580.  * sorted search" idea described by Heckbert and on the incremental distance
  581.  * calculation described by Spencer W. Thomas in chapter III.1 of Graphics
  582.  * Gems II (James Arvo, ed.  Academic Press, 1991).  Thomas points out that
  583.  * the distances from a given colormap entry to each cell of the histogram can
  584.  * be computed quickly using an incremental method: the differences between
  585.  * distances to adjacent cells themselves differ by a constant.  This allows a
  586.  * fairly fast implementation of the "brute force" approach of computing the
  587.  * distance from every colormap entry to every histogram cell.  Unfortunately,
  588.  * it needs a work array to hold the best-distance-so-far for each histogram
  589.  * cell (because the inner loop has to be over cells, not colormap entries).
  590.  * The work array elements have to be INT32s, so the work array would need
  591.  * 256Kb at our recommended precision.  This is not feasible in DOS machines.
  592.  *
  593.  * To get around these problems, we apply Thomas' method to compute the
  594.  * nearest colors for only the cells within a small subbox of the histogram.
  595.  * The work array need be only as big as the subbox, so the memory usage
  596.  * problem is solved.  Furthermore, we need not fill subboxes that are never
  597.  * referenced in pass2; many images use only part of the color gamut, so a
  598.  * fair amount of work is saved.  An additional advantage of this
  599.  * approach is that we can apply Heckbert's locality criterion to quickly
  600.  * eliminate colormap entries that are far away from the subbox; typically
  601.  * three-fourths of the colormap entries are rejected by Heckbert's criterion,
  602.  * and we need not compute their distances to individual cells in the subbox.
  603.  * The speed of this approach is heavily influenced by the subbox size: too
  604.  * small means too much overhead, too big loses because Heckbert's criterion
  605.  * can't eliminate as many colormap entries.  Empirically the best subbox
  606.  * size seems to be about 1/512th of the histogram (1/8th in each direction).
  607.  *
  608.  * Thomas' article also describes a refined method which is asymptotically
  609.  * faster than the brute-force method, but it is also far more complex and
  610.  * cannot efficiently be applied to small subboxes.  It is therefore not
  611.  * useful for programs intended to be portable to DOS machines.  On machines
  612.  * with plenty of memory, filling the whole histogram in one shot with Thomas'
  613.  * refined method might be faster than the present code --- but then again,
  614.  * it might not be any faster, and it's certainly more complicated.
  615.  */
  616.  
  617.  
  618. /* log2(histogram cells in update box) for each axis; this can be adjusted */
  619. #define BOX_C0_LOG  (HIST_C0_BITS-3)
  620. #define BOX_C1_LOG  (HIST_C1_BITS-3)
  621. #define BOX_C2_LOG  (HIST_C2_BITS-3)
  622.  
  623. #define BOX_C0_ELEMS  (1<<BOX_C0_LOG) /* # of hist cells in update box */
  624. #define BOX_C1_ELEMS  (1<<BOX_C1_LOG)
  625. #define BOX_C2_ELEMS  (1<<BOX_C2_LOG)
  626.  
  627. #define BOX_C0_SHIFT  (C0_SHIFT + BOX_C0_LOG)
  628. #define BOX_C1_SHIFT  (C1_SHIFT + BOX_C1_LOG)
  629. #define BOX_C2_SHIFT  (C2_SHIFT + BOX_C2_LOG)
  630.  
  631.  
  632. /*
  633.  * The next three routines implement inverse colormap filling.  They could
  634.  * all be folded into one big routine, but splitting them up this way saves
  635.  * some stack space (the mindist[] and bestdist[] arrays need not coexist)
  636.  * and may allow some compilers to produce better code by registerizing more
  637.  * inner-loop variables.
  638.  */
  639.  
  640. LOCAL int
  641. find_nearby_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
  642.             JSAMPLE colorlist[])
  643. /* Locate the colormap entries close enough to an update box to be candidates
  644.  * for the nearest entry to some cell(s) in the update box.  The update box
  645.  * is specified by the center coordinates of its first cell.  The number of
  646.  * candidate colormap entries is returned, and their colormap indexes are
  647.  * placed in colorlist[].
  648.  * This routine uses Heckbert's "locally sorted search" criterion to select
  649.  * the colors that need further consideration.
  650.  */
  651. {
  652.   int numcolors = cinfo->actual_number_of_colors;
  653.   int maxc0, maxc1, maxc2;
  654.   int centerc0, centerc1, centerc2;
  655.   int i, x, ncolors;
  656.   INT32 minmaxdist, min_dist, max_dist, tdist;
  657.   INT32 mindist[MAXNUMCOLORS];    /* min distance to colormap entry i */
  658.  
  659.   /* Compute true coordinates of update box's upper corner and center.
  660.    * Actually we compute the coordinates of the center of the upper-corner
  661.    * histogram cell, which are the upper bounds of the volume we care about.
  662.    * Note that since ">>" rounds down, the "center" values may be closer to
  663.    * min than to max; hence comparisons to them must be "<=", not "<".
  664.    */
  665.   maxc0 = minc0 + ((1 << BOX_C0_SHIFT) - (1 << C0_SHIFT));
  666.   centerc0 = (minc0 + maxc0) >> 1;
  667.   maxc1 = minc1 + ((1 << BOX_C1_SHIFT) - (1 << C1_SHIFT));
  668.   centerc1 = (minc1 + maxc1) >> 1;
  669.   maxc2 = minc2 + ((1 << BOX_C2_SHIFT) - (1 << C2_SHIFT));
  670.   centerc2 = (minc2 + maxc2) >> 1;
  671.  
  672.   /* For each color in colormap, find:
  673.    *  1. its minimum squared-distance to any point in the update box
  674.    *     (zero if color is within update box);
  675.    *  2. its maximum squared-distance to any point in the update box.
  676.    * Both of these can be found by considering only the corners of the box.
  677.    * We save the minimum distance for each color in mindist[];
  678.    * only the smallest maximum distance is of interest.
  679.    */
  680.   minmaxdist = 0x7FFFFFFFL;
  681.  
  682.   for (i = 0; i < numcolors; i++) {
  683.     /* We compute the squared-c0-distance term, then add in the other two. */
  684.     x = GETJSAMPLE(cinfo->colormap[0][i]);
  685.     if (x < minc0) {
  686.       tdist = (x - minc0) * C0_SCALE;
  687.       min_dist = tdist*tdist;
  688.       tdist = (x - maxc0) * C0_SCALE;
  689.       max_dist = tdist*tdist;
  690.     } else if (x > maxc0) {
  691.       tdist = (x - maxc0) * C0_SCALE;
  692.       min_dist = tdist*tdist;
  693.       tdist = (x - minc0) * C0_SCALE;
  694.       max_dist = tdist*tdist;
  695.     } else {
  696.       /* within cell range so no contribution to min_dist */
  697.       min_dist = 0;
  698.       if (x <= centerc0) {
  699.     tdist = (x - maxc0) * C0_SCALE;
  700.     max_dist = tdist*tdist;
  701.       } else {
  702.     tdist = (x - minc0) * C0_SCALE;
  703.     max_dist = tdist*tdist;
  704.       }
  705.     }
  706.  
  707.     x = GETJSAMPLE(cinfo->colormap[1][i]);
  708.     if (x < minc1) {
  709.       tdist = (x - minc1) * C1_SCALE;
  710.       min_dist += tdist*tdist;
  711.       tdist = (x - maxc1) * C1_SCALE;
  712.       max_dist += tdist*tdist;
  713.     } else if (x > maxc1) {
  714.       tdist = (x - maxc1) * C1_SCALE;
  715.       min_dist += tdist*tdist;
  716.       tdist = (x - minc1) * C1_SCALE;
  717.       max_dist += tdist*tdist;
  718.     } else {
  719.       /* within cell range so no contribution to min_dist */
  720.       if (x <= centerc1) {
  721.     tdist = (x - maxc1) * C1_SCALE;
  722.     max_dist += tdist*tdist;
  723.       } else {
  724.     tdist = (x - minc1) * C1_SCALE;
  725.     max_dist += tdist*tdist;
  726.       }
  727.     }
  728.  
  729.     x = GETJSAMPLE(cinfo->colormap[2][i]);
  730.     if (x < minc2) {
  731.       tdist = (x - minc2) * C2_SCALE;
  732.       min_dist += tdist*tdist;
  733.       tdist = (x - maxc2) * C2_SCALE;
  734.       max_dist += tdist*tdist;
  735.     } else if (x > maxc2) {
  736.       tdist = (x - maxc2) * C2_SCALE;
  737.       min_dist += tdist*tdist;
  738.       tdist = (x - minc2) * C2_SCALE;
  739.       max_dist += tdist*tdist;
  740.     } else {
  741.       /* within cell range so no contribution to min_dist */
  742.       if (x <= centerc2) {
  743.     tdist = (x - maxc2) * C2_SCALE;
  744.     max_dist += tdist*tdist;
  745.       } else {
  746.     tdist = (x - minc2) * C2_SCALE;
  747.     max_dist += tdist*tdist;
  748.       }
  749.     }
  750.  
  751.     mindist[i] = min_dist;    /* save away the results */
  752.     if (max_dist < minmaxdist)
  753.       minmaxdist = max_dist;
  754.   }
  755.  
  756.   /* Now we know that no cell in the update box is more than minmaxdist
  757.    * away from some colormap entry.  Therefore, only colors that are
  758.    * within minmaxdist of some part of the box need be considered.
  759.    */
  760.   ncolors = 0;
  761.   for (i = 0; i < numcolors; i++) {
  762.     if (mindist[i] <= minmaxdist)
  763.       colorlist[ncolors++] = (JSAMPLE) i;
  764.   }
  765.   return ncolors;
  766. }
  767.  
  768.  
  769. LOCAL void
  770. find_best_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
  771.           int numcolors, JSAMPLE colorlist[], JSAMPLE bestcolor[])
  772. /* Find the closest colormap entry for each cell in the update box,
  773.  * given the list of candidate colors prepared by find_nearby_colors.
  774.  * Return the indexes of the closest entries in the bestcolor[] array.
  775.  * This routine uses Thomas' incremental distance calculation method to
  776.  * find the distance from a colormap entry to successive cells in the box.
  777.  */
  778. {
  779.   int ic0, ic1, ic2;
  780.   int i, icolor;
  781.   register INT32 * bptr;    /* pointer into bestdist[] array */
  782.   JSAMPLE * cptr;        /* pointer into bestcolor[] array */
  783.   INT32 dist0, dist1;        /* initial distance values */
  784.   register INT32 dist2;        /* current distance in inner loop */
  785.   INT32 xx0, xx1;        /* distance increments */
  786.   register INT32 xx2;
  787.   INT32 inc0, inc1, inc2;    /* initial values for increments */
  788.   /* This array holds the distance to the nearest-so-far color for each cell */
  789.   INT32 bestdist[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];
  790.  
  791.   /* Initialize best-distance for each cell of the update box */
  792.   bptr = bestdist;
  793.   for (i = BOX_C0_ELEMS*BOX_C1_ELEMS*BOX_C2_ELEMS-1; i >= 0; i--)
  794.     *bptr++ = 0x7FFFFFFFL;
  795.   
  796.   /* For each color selected by find_nearby_colors,
  797.    * compute its distance to the center of each cell in the box.
  798.    * If that's less than best-so-far, update best distance and color number.
  799.    */
  800.   
  801.   /* Nominal steps between cell centers ("x" in Thomas article) */
  802. #define STEP_C0  ((1 << C0_SHIFT) * C0_SCALE)
  803. #define STEP_C1  ((1 << C1_SHIFT) * C1_SCALE)
  804. #define STEP_C2  ((1 << C2_SHIFT) * C2_SCALE)
  805.   
  806.   for (i = 0; i < numcolors; i++) {
  807.     icolor = GETJSAMPLE(colorlist[i]);
  808.     /* Compute (square of) distance from minc0/c1/c2 to this color */
  809.     inc0 = (minc0 - GETJSAMPLE(cinfo->colormap[0][icolor])) * C0_SCALE;
  810.     dist0 = inc0*inc0;
  811.     inc1 = (minc1 - GETJSAMPLE(cinfo->colormap[1][icolor])) * C1_SCALE;
  812.     dist0 += inc1*inc1;
  813.     inc2 = (minc2 - GETJSAMPLE(cinfo->colormap[2][icolor])) * C2_SCALE;
  814.     dist0 += inc2*inc2;
  815.     /* Form the initial difference increments */
  816.     inc0 = inc0 * (2 * STEP_C0) + STEP_C0 * STEP_C0;
  817.     inc1 = inc1 * (2 * STEP_C1) + STEP_C1 * STEP_C1;
  818.     inc2 = inc2 * (2 * STEP_C2) + STEP_C2 * STEP_C2;
  819.     /* Now loop over all cells in box, updating distance per Thomas method */
  820.     bptr = bestdist;
  821.     cptr = bestcolor;
  822.     xx0 = inc0;
  823.     for (ic0 = BOX_C0_ELEMS-1; ic0 >= 0; ic0--) {
  824.       dist1 = dist0;
  825.       xx1 = inc1;
  826.       for (ic1 = BOX_C1_ELEMS-1; ic1 >= 0; ic1--) {
  827.     dist2 = dist1;
  828.     xx2 = inc2;
  829.     for (ic2 = BOX_C2_ELEMS-1; ic2 >= 0; ic2--) {
  830.       if (dist2 < *bptr) {
  831.         *bptr = dist2;
  832.         *cptr = (JSAMPLE) icolor;
  833.       }
  834.       dist2 += xx2;
  835.       xx2 += 2 * STEP_C2 * STEP_C2;
  836.       bptr++;
  837.       cptr++;
  838.     }
  839.     dist1 += xx1;
  840.     xx1 += 2 * STEP_C1 * STEP_C1;
  841.       }
  842.       dist0 += xx0;
  843.       xx0 += 2 * STEP_C0 * STEP_C0;
  844.     }
  845.   }
  846. }
  847.  
  848.  
  849. LOCAL void
  850. fill_inverse_cmap (j_decompress_ptr cinfo, int c0, int c1, int c2)
  851. /* Fill the inverse-colormap entries in the update box that contains */
  852. /* histogram cell c0/c1/c2.  (Only that one cell MUST be filled, but */
  853. /* we can fill as many others as we wish.) */
  854. {
  855.   my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  856.   hist3d histogram = cquantize->histogram;
  857.   int minc0, minc1, minc2;    /* lower left corner of update box */
  858.   int ic0, ic1, ic2;
  859.   register JSAMPLE * cptr;    /* pointer into bestcolor[] array */
  860.   register histptr cachep;    /* pointer into main cache array */
  861.   /* This array lists the candidate colormap indexes. */
  862.   JSAMPLE colorlist[MAXNUMCOLORS];
  863.   int numcolors;        /* number of candidate colors */
  864.   /* This array holds the actually closest colormap index for each cell. */
  865.   JSAMPLE bestcolor[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];
  866.  
  867.   /* Convert cell coordinates to update box ID */
  868.   c0 >>= BOX_C0_LOG;
  869.   c1 >>= BOX_C1_LOG;
  870.   c2 >>= BOX_C2_LOG;
  871.  
  872.   /* Compute true coordinates of update box's origin corner.
  873.    * Actually we compute the coordinates of the center of the corner
  874.    * histogram cell, which are the lower bounds of the volume we care about.
  875.    */
  876.   minc0 = (c0 << BOX_C0_SHIFT) + ((1 << C0_SHIFT) >> 1);
  877.   minc1 = (c1 << BOX_C1_SHIFT) + ((1 << C1_SHIFT) >> 1);
  878.   minc2 = (c2 << BOX_C2_SHIFT) + ((1 << C2_SHIFT) >> 1);
  879.   
  880.   /* Determine which colormap entries are close enough to be candidates
  881.    * for the nearest entry to some cell in the update box.
  882.    */
  883.   numcolors = find_nearby_colors(cinfo, minc0, minc1, minc2, colorlist);
  884.  
  885.   /* Determine the actually nearest colors. */
  886.   find_best_colors(cinfo, minc0, minc1, minc2, numcolors, colorlist,
  887.            bestcolor);
  888.  
  889.   /* Save the best color numbers (plus 1) in the main cache array */
  890.   c0 <<= BOX_C0_LOG;        /* convert ID back to base cell indexes */
  891.   c1 <<= BOX_C1_LOG;
  892.   c2 <<= BOX_C2_LOG;
  893.   cptr = bestcolor;
  894.   for (ic0 = 0; ic0 < BOX_C0_ELEMS; ic0++) {
  895.     for (ic1 = 0; ic1 < BOX_C1_ELEMS; ic1++) {
  896.       cachep = & histogram[c0+ic0][c1+ic1][c2];
  897.       for (ic2 = 0; ic2 < BOX_C2_ELEMS; ic2++) {
  898.     *cachep++ = (histcell) (GETJSAMPLE(*cptr++) + 1);
  899.       }
  900.     }
  901.   }
  902. }
  903.  
  904.  
  905. /*
  906.  * Map some rows of pixels to the output colormapped representation.
  907.  */
  908.  
  909. METHODDEF void
  910. pass2_no_dither (j_decompress_ptr cinfo,
  911.          JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
  912. /* This version performs no dithering */
  913. {
  914.   my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  915.   hist3d histogram = cquantize->histogram;
  916.   register JSAMPROW inptr, outptr;
  917.   register histptr cachep;
  918.   register int c0, c1, c2;
  919.   int row;
  920.   JDIMENSION col;
  921.   JDIMENSION width = cinfo->output_width;
  922.  
  923.   for (row = 0; row < num_rows; row++) {
  924.     inptr = input_buf[row];
  925.     outptr = output_buf[row];
  926.     for (col = width; col > 0; col--) {
  927.       /* get pixel value and index into the cache */
  928.       c0 = GETJSAMPLE(*inptr++) >> C0_SHIFT;
  929.       c1 = GETJSAMPLE(*inptr++) >> C1_SHIFT;
  930.       c2 = GETJSAMPLE(*inptr++) >> C2_SHIFT;
  931.       cachep = & histogram[c0][c1][c2];
  932.       /* If we have not seen this color before, find nearest colormap entry */
  933.       /* and update the cache */
  934.       if (*cachep == 0)
  935.     fill_inverse_cmap(cinfo, c0,c1,c2);
  936.       /* Now emit the colormap index for this cell */
  937.       *outptr++ = (JSAMPLE) (*cachep - 1);
  938.     }
  939.   }
  940. }
  941.  
  942.  
  943. METHODDEF void
  944. pass2_fs_dither (j_decompress_ptr cinfo,
  945.          JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
  946. /* This version performs Floyd-Steinberg dithering */
  947. {
  948.   my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  949.   hist3d histogram = cquantize->histogram;
  950.   register LOCFSERROR cur0, cur1, cur2;    /* current error or pixel value */
  951.   LOCFSERROR belowerr0, belowerr1, belowerr2; /* error for pixel below cur */
  952.   LOCFSERROR bpreverr0, bpreverr1, bpreverr2; /* error for below/prev col */
  953.   register FSERRPTR errorptr;    /* => fserrors[] at column before current */
  954.   JSAMPROW inptr;        /* => current input pixel */
  955.   JSAMPROW outptr;        /* => current output pixel */
  956.   histptr cachep;
  957.   int dir;            /* +1 or -1 depending on direction */
  958.   int dir3;            /* 3*dir, for advancing inptr & errorptr */
  959.   int row;
  960.   JDIMENSION col;
  961.   JDIMENSION width = cinfo->output_width;
  962.   JSAMPLE *range_limit = cinfo->sample_range_limit;
  963.   int *error_limit = cquantize->error_limiter;
  964.   JSAMPROW colormap0 = cinfo->colormap[0];
  965.   JSAMPROW colormap1 = cinfo->colormap[1];
  966.   JSAMPROW colormap2 = cinfo->colormap[2];
  967.   SHIFT_TEMPS
  968.  
  969.   for (row = 0; row < num_rows; row++) {
  970.     inptr = input_buf[row];
  971.     outptr = output_buf[row];
  972.     if (cquantize->on_odd_row) {
  973.       /* work right to left in this row */
  974.       inptr += (width-1) * 3;    /* so point to rightmost pixel */
  975.       outptr += width-1;
  976.       dir = -1;
  977.       dir3 = -3;
  978.       errorptr = cquantize->fserrors + (width+1)*3; /* => entry after last column */
  979.       cquantize->on_odd_row = FALSE; /* flip for next time */
  980.     } else {
  981.       /* work left to right in this row */
  982.       dir = 1;
  983.       dir3 = 3;
  984.       errorptr = cquantize->fserrors; /* => entry before first real column */
  985.       cquantize->on_odd_row = TRUE; /* flip for next time */
  986.     }
  987.     /* Preset error values: no error propagated to first pixel from left */
  988.     cur0 = cur1 = cur2 = 0;
  989.     /* and no error propagated to row below yet */
  990.     belowerr0 = belowerr1 = belowerr2 = 0;
  991.     bpreverr0 = bpreverr1 = bpreverr2 = 0;
  992.  
  993.     for (col = width; col > 0; col--) {
  994.       /* curN holds the error propagated from the previous pixel on the
  995.        * current line.  Add the error propagated from the previous line
  996.        * to form the complete error correction term for this pixel, and
  997.        * round the error term (which is expressed * 16) to an integer.
  998.        * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct
  999.        * for either sign of the error value.
  1000.        * Note: errorptr points to *previous* column's array entry.
  1001.        */
  1002.       cur0 = RIGHT_SHIFT(cur0 + errorptr[dir3+0] + 8, 4);
  1003.       cur1 = RIGHT_SHIFT(cur1 + errorptr[dir3+1] + 8, 4);
  1004.       cur2 = RIGHT_SHIFT(cur2 + errorptr[dir3+2] + 8, 4);
  1005.       /* Limit the error using transfer function set by init_error_limit.
  1006.        * See comments with init_error_limit for rationale.
  1007.        */
  1008.       cur0 = error_limit[cur0];
  1009.       cur1 = error_limit[cur1];
  1010.       cur2 = error_limit[cur2];
  1011.       /* Form pixel value + error, and range-limit to 0..MAXJSAMPLE.
  1012.        * The maximum error is +- MAXJSAMPLE (or less with error limiting);
  1013.        * this sets the required size of the range_limit array.
  1014.        */
  1015.       cur0 += GETJSAMPLE(inptr[0]);
  1016.       cur1 += GETJSAMPLE(inptr[1]);
  1017.       cur2 += GETJSAMPLE(inptr[2]);
  1018.       cur0 = GETJSAMPLE(range_limit[cur0]);
  1019.       cur1 = GETJSAMPLE(range_limit[cur1]);
  1020.       cur2 = GETJSAMPLE(range_limit[cur2]);
  1021.       /* Index into the cache with adjusted pixel value */
  1022.       cachep = & histogram[cur0>>C0_SHIFT][cur1>>C1_SHIFT][cur2>>C2_SHIFT];
  1023.       /* If we have not seen this color before, find nearest colormap */
  1024.       /* entry and update the cache */
  1025.       if (*cachep == 0)
  1026.     fill_inverse_cmap(cinfo, cur0>>C0_SHIFT,cur1>>C1_SHIFT,cur2>>C2_SHIFT);
  1027.       /* Now emit the colormap index for this cell */
  1028.       { register int pixcode = *cachep - 1;
  1029.     *outptr = (JSAMPLE) pixcode;
  1030.     /* Compute representation error for this pixel */
  1031.     cur0 -= GETJSAMPLE(colormap0[pixcode]);
  1032.     cur1 -= GETJSAMPLE(colormap1[pixcode]);
  1033.     cur2 -= GETJSAMPLE(colormap2[pixcode]);
  1034.       }
  1035.       /* Compute error fractions to be propagated to adjacent pixels.
  1036.        * Add these into the running sums, and simultaneously shift the
  1037.        * next-line error sums left by 1 column.
  1038.        */
  1039.       { register LOCFSERROR bnexterr, delta;
  1040.  
  1041.     bnexterr = cur0;    /* Process component 0 */
  1042.     delta = cur0 * 2;
  1043.     cur0 += delta;        /* form error * 3 */
  1044.     errorptr[0] = (FSERROR) (bpreverr0 + cur0);
  1045.     cur0 += delta;        /* form error * 5 */
  1046.     bpreverr0 = belowerr0 + cur0;
  1047.     belowerr0 = bnexterr;
  1048.     cur0 += delta;        /* form error * 7 */
  1049.     bnexterr = cur1;    /* Process component 1 */
  1050.     delta = cur1 * 2;
  1051.     cur1 += delta;        /* form error * 3 */
  1052.     errorptr[1] = (FSERROR) (bpreverr1 + cur1);
  1053.     cur1 += delta;        /* form error * 5 */
  1054.     bpreverr1 = belowerr1 + cur1;
  1055.     belowerr1 = bnexterr;
  1056.     cur1 += delta;        /* form error * 7 */
  1057.     bnexterr = cur2;    /* Process component 2 */
  1058.     delta = cur2 * 2;
  1059.     cur2 += delta;        /* form error * 3 */
  1060.     errorptr[2] = (FSERROR) (bpreverr2 + cur2);
  1061.     cur2 += delta;        /* form error * 5 */
  1062.     bpreverr2 = belowerr2 + cur2;
  1063.     belowerr2 = bnexterr;
  1064.     cur2 += delta;        /* form error * 7 */
  1065.       }
  1066.       /* At this point curN contains the 7/16 error value to be propagated
  1067.        * to the next pixel on the current line, and all the errors for the
  1068.        * next line have been shifted over.  We are therefore ready to move on.
  1069.        */
  1070.       inptr += dir3;        /* Advance pixel pointers to next column */
  1071.       outptr += dir;
  1072.       errorptr += dir3;        /* advance errorptr to current column */
  1073.     }
  1074.     /* Post-loop cleanup: we must unload the final error values into the
  1075.      * final fserrors[] entry.  Note we need not unload belowerrN because
  1076.      * it is for the dummy column before or after the actual array.
  1077.      */
  1078.     errorptr[0] = (FSERROR) bpreverr0; /* unload prev errs into array */
  1079.     errorptr[1] = (FSERROR) bpreverr1;
  1080.     errorptr[2] = (FSERROR) bpreverr2;
  1081.   }
  1082. }
  1083.  
  1084.  
  1085. /*
  1086.  * Initialize the error-limiting transfer function (lookup table).
  1087.  * The raw F-S error computation can potentially compute error values of up to
  1088.  * +- MAXJSAMPLE.  But we want the maximum correction applied to a pixel to be
  1089.  * much less, otherwise obviously wrong pixels will be created.  (Typical
  1090.  * effects include weird fringes at color-area boundaries, isolated bright
  1091.  * pixels in a dark area, etc.)  The standard advice for avoiding this problem
  1092.  * is to ensure that the "corners" of the color cube are allocated as output
  1093.  * colors; then repeated errors in the same direction cannot cause cascading
  1094.  * error buildup.  However, that only prevents the error from getting
  1095.  * completely out of hand; Aaron Giles reports that error limiting improves
  1096.  * the results even with corner colors allocated.
  1097.  * A simple clamping of the error values to about +- MAXJSAMPLE/8 works pretty
  1098.  * well, but the smoother transfer function used below is even better.  Thanks
  1099.  * to Aaron Giles for this idea.
  1100.  */
  1101.  
  1102. LOCAL void
  1103. init_error_limit (j_decompress_ptr cinfo)
  1104. /* Allocate and fill in the error_limiter table */
  1105. {
  1106.   my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  1107.   int * table;
  1108.   int in, out;
  1109.  
  1110.   table = (int *) (*cinfo->mem->alloc_small)
  1111.     ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE*2+1) * SIZEOF(int));
  1112.   table += MAXJSAMPLE;        /* so can index -MAXJSAMPLE .. +MAXJSAMPLE */
  1113.   cquantize->error_limiter = table;
  1114.  
  1115. #define STEPSIZE ((MAXJSAMPLE+1)/16)
  1116.   /* Map errors 1:1 up to +- MAXJSAMPLE/16 */
  1117.   out = 0;
  1118.   for (in = 0; in < STEPSIZE; in++, out++) {
  1119.     table[in] = out; table[-in] = -out;
  1120.   }
  1121.   /* Map errors 1:2 up to +- 3*MAXJSAMPLE/16 */
  1122.   for (; in < STEPSIZE*3; in++, out += (in&1) ? 0 : 1) {
  1123.     table[in] = out; table[-in] = -out;
  1124.   }
  1125.   /* Clamp the rest to final out value (which is (MAXJSAMPLE+1)/8) */
  1126.   for (; in <= MAXJSAMPLE; in++) {
  1127.     table[in] = out; table[-in] = -out;
  1128.   }
  1129. #undef STEPSIZE
  1130. }
  1131.  
  1132.  
  1133. /*
  1134.  * Finish up at the end of each pass.
  1135.  */
  1136.  
  1137. METHODDEF void
  1138. finish_pass1 (j_decompress_ptr cinfo)
  1139. {
  1140.   /* Select the representative colors and fill in cinfo->colormap */
  1141.   select_colors(cinfo);
  1142. }
  1143.  
  1144.  
  1145. METHODDEF void
  1146. finish_pass2 (j_decompress_ptr cinfo)
  1147. {
  1148.   /* no work */
  1149. }
  1150.  
  1151.  
  1152. /*
  1153.  * Initialize for each processing pass.
  1154.  */
  1155.  
  1156. METHODDEF void
  1157. start_pass_2_quant (j_decompress_ptr cinfo, boolean is_pre_scan)
  1158. {
  1159.   my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  1160.   hist3d histogram = cquantize->histogram;
  1161.   int i;
  1162.  
  1163.   if (is_pre_scan) {
  1164.     /* Set up method pointers */
  1165.     cquantize->pub.color_quantize = prescan_quantize;
  1166.     cquantize->pub.finish_pass = finish_pass1;
  1167.   } else {
  1168.     /* Set up method pointers */
  1169.     if (cinfo->dither_mode == JDITHER_FS)
  1170.       cquantize->pub.color_quantize = pass2_fs_dither;
  1171.     else
  1172.       cquantize->pub.color_quantize = pass2_no_dither;
  1173.     cquantize->pub.finish_pass = finish_pass2;
  1174.   }
  1175.   /* Zero the histogram or inverse color map */
  1176.   for (i = 0; i < HIST_C0_ELEMS; i++) {
  1177.     jzero_far((void FAR *) histogram[i],
  1178.           HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell));
  1179.   }
  1180. }
  1181.  
  1182.  
  1183. /*
  1184.  * Module initialization routine for 2-pass color quantization.
  1185.  */
  1186.  
  1187. GLOBAL void
  1188. jinit_2pass_quantizer (j_decompress_ptr cinfo)
  1189. {
  1190.   my_cquantize_ptr cquantize;
  1191.   int i;
  1192.  
  1193.   cquantize = (my_cquantize_ptr)
  1194.     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  1195.                 SIZEOF(my_cquantizer));
  1196.   cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize;
  1197.   cquantize->pub.start_pass = start_pass_2_quant;
  1198.  
  1199.   /* Make sure jdmaster didn't give me a case I can't handle */
  1200.   if (cinfo->out_color_components != 3)
  1201.     ERREXIT(cinfo, JERR_NOTIMPL);
  1202.  
  1203.   /* Only F-S dithering or no dithering is supported. */
  1204.   /* If user asks for ordered dither, give him F-S. */
  1205.   if (cinfo->dither_mode != JDITHER_NONE)
  1206.     cinfo->dither_mode = JDITHER_FS;
  1207.  
  1208.   /* Make sure color count is acceptable */
  1209.   i = (cinfo->colormap != NULL) ? cinfo->actual_number_of_colors
  1210.                 : cinfo->desired_number_of_colors;
  1211.   /* Lower bound on # of colors ... somewhat arbitrary as long as > 0 */
  1212.   if (i < 8)
  1213.     ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 8);
  1214.   /* Make sure colormap indexes can be represented by JSAMPLEs */
  1215.   if (i > MAXNUMCOLORS)
  1216.     ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS);
  1217.  
  1218.   /* Allocate the histogram/inverse colormap storage */
  1219.   cquantize->histogram = (hist3d) (*cinfo->mem->alloc_small)
  1220.     ((j_common_ptr) cinfo, JPOOL_IMAGE, HIST_C0_ELEMS * SIZEOF(hist2d));
  1221.   for (i = 0; i < HIST_C0_ELEMS; i++) {
  1222.     cquantize->histogram[i] = (hist2d) (*cinfo->mem->alloc_large)
  1223.       ((j_common_ptr) cinfo, JPOOL_IMAGE,
  1224.        HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell));
  1225.   }
  1226.  
  1227.   /* Allocate storage for the completed colormap,
  1228.    * unless it has been supplied by the application.
  1229.    * We do this now since it is FAR storage and may affect
  1230.    * the memory manager's space calculations.
  1231.    */
  1232.   if (cinfo->colormap == NULL) {
  1233.     cinfo->colormap = (*cinfo->mem->alloc_sarray)
  1234.       ((j_common_ptr) cinfo, JPOOL_IMAGE,
  1235.        (JDIMENSION) cinfo->desired_number_of_colors, (JDIMENSION) 3);
  1236.   }
  1237.  
  1238.   /* Allocate Floyd-Steinberg workspace if necessary. */
  1239.   /* This isn't needed until pass 2, but again it is FAR storage. */
  1240.   if (cinfo->dither_mode == JDITHER_FS) {
  1241.     size_t arraysize = (size_t) ((cinfo->output_width + 2) *
  1242.                  (3 * SIZEOF(FSERROR)));
  1243.  
  1244.     cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
  1245.       ((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize);
  1246.     /* Initialize the propagated errors to zero. */
  1247.     jzero_far((void FAR *) cquantize->fserrors, arraysize);
  1248.     cquantize->on_odd_row = FALSE;
  1249.     init_error_limit(cinfo);
  1250.   }
  1251. }
  1252.  
  1253. #endif /* QUANT_2PASS_SUPPORTED */
  1254.